package com.mr3chi.horoscopes;

import java.util.ArrayList;

import com.mr3chi.horoscopes.R;
import com.nhaarman.listviewanimations.swinginadapters.AnimationAdapter;
import com.nhaarman.listviewanimations.swinginadapters.prepared.AlphaInAnimationAdapter;
import com.nhaarman.listviewanimations.swinginadapters.prepared.SwingBottomInAnimationAdapter;
import com.nhaarman.listviewanimations.swinginadapters.prepared.SwingLeftInAnimationAdapter;
import com.nhaarman.listviewanimations.swinginadapters.prepared.SwingRightInAnimationAdapter;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

public class FragmentHoroscopes extends Fragment{
	GridView gridView;
	ArrayList<CustomGridItem> gridArray;
	CustomGridViewAdapter customGridAdapter;
	
	public FragmentHoroscopes() {
		
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		View rootView = inflater.inflate(R.layout.fragment_horoscopes, container, false);
		
		gridArray = new ArrayList<CustomGridItem>();
		Bitmap icon = BitmapFactory.decodeResource(this.getResources(), R.drawable.p1_aries);
		gridArray.add(new CustomGridItem(icon,"Aries"));
		
		icon = BitmapFactory.decodeResource(this.getResources(), R.drawable.p2_taurus);
		gridArray.add(new CustomGridItem(icon,"Taurus"));
		
		icon = BitmapFactory.decodeResource(this.getResources(), R.drawable.p3_gemini);
		gridArray.add(new CustomGridItem(icon,"Gemini"));
		
		icon = BitmapFactory.decodeResource(this.getResources(), R.drawable.p4_cancer);
		gridArray.add(new CustomGridItem(icon,"Cancer"));
		
		icon = BitmapFactory.decodeResource(this.getResources(), R.drawable.p5_leo);
		gridArray.add(new CustomGridItem(icon,"Leo"));
		
		icon = BitmapFactory.decodeResource(this.getResources(), R.drawable.p6_virgo);
		gridArray.add(new CustomGridItem(icon,"Virgo"));
		
		icon = BitmapFactory.decodeResource(this.getResources(), R.drawable.p7_libra);
		gridArray.add(new CustomGridItem(icon,"Libra"));
		
		icon = BitmapFactory.decodeResource(this.getResources(), R.drawable.p8_scorpio);
		gridArray.add(new CustomGridItem(icon,"Scorpio"));
		
		icon = BitmapFactory.decodeResource(this.getResources(), R.drawable.p9_sagittarius);
		gridArray.add(new CustomGridItem(icon,"Sagittarius"));
		
		icon = BitmapFactory.decodeResource(this.getResources(), R.drawable.p10_capricorn);
		gridArray.add(new CustomGridItem(icon,"Capricorn"));
		
		icon = BitmapFactory.decodeResource(this.getResources(), R.drawable.p11_aquarius);
		gridArray.add(new CustomGridItem(icon,"Aquarius"));
		
		icon = BitmapFactory.decodeResource(this.getResources(), R.drawable.p12_pisces);
		gridArray.add(new CustomGridItem(icon,"Pisces"));
		
		gridView = (GridView) rootView.findViewById(R.id.gridView1);
		
		customGridAdapter = new CustomGridViewAdapter(getActivity(), R.layout.item_grid, gridArray);
		
		SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getActivity());
		String strAni = settings.getString("setting_effect", "0");
		int intAni = Integer.parseInt(strAni);
		
		AnimationAdapter aniAdapter;
		
		switch(intAni){
		case 1:
			aniAdapter = new AlphaInAnimationAdapter(customGridAdapter);
			break;
		case 2:
			aniAdapter = new SwingLeftInAnimationAdapter(customGridAdapter);
			break;
		case 3:
			aniAdapter = new SwingRightInAnimationAdapter(customGridAdapter);
			break;
		default:
			aniAdapter = new SwingBottomInAnimationAdapter(customGridAdapter);
			break;
		}
		aniAdapter.setAbsListView(gridView);
		aniAdapter.setInitialDelayMillis(200);
		gridView.setAdapter(aniAdapter);
		
		gridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent(getActivity(), ActivityHoroscopeDetail.class);
				intent.putExtra(ActivityHoroscopeDetail.ARG_NAME, gridArray.get(position).title);
				startActivity(intent);
			}
		});
        return rootView;
	}
	
}
